package io.arukas.endpoint;

import io.arukas.domain.SysAdmin;
import io.arukas.domain.SysUser;
import io.arukas.repository.SysAdminRepository;
import io.arukas.repository.SysUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

/**
 * Created by IntelliJ IDEA. <br/>
 * Author: YuXian Niu <br/>
 * Email: niuyuxian@163.com <br/>
 * Create: 2021-08-15 22:23 <br/>
 */
@RestController
@RequestMapping("account")
public class AccountEndpoint {

    @Autowired
    private SysAdminRepository sysAdminRepository;
    @Autowired
    private SysUserRepository sysUserRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @GetMapping(value = "hello")
    public String hello() {
        return "hello account";
    }

    @PostMapping(value = "admin")
    public ResponseEntity<SysAdmin> createAdmin(@RequestBody SysAdmin sysAdmin) {

        sysAdmin.setPassword(passwordEncoder.encode(sysAdmin.getPassword()));
        sysAdmin = sysAdminRepository.save(sysAdmin);
        return ResponseEntity.ok(sysAdmin);
    }

    @GetMapping(value = "user")
    public ResponseEntity<SysUser> createAdmin(SysUser sysUser) {

        sysUser.setPassword(passwordEncoder.encode(sysUser.getPassword()));
        sysUser = sysUserRepository.save(sysUser);
        return ResponseEntity.ok(sysUser);
    }
}
